using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ConsoleApp10;

namespace WebApplication2.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class OrderController : ControllerBase
    {
        private readonly OrderContext orderingContext;

        public OrderController(OrderContext newone)
        {
            orderingContext = newone;
        }
        [HttpGet]
        public ActionResult<List<Order>> GetOrders(int? id, string? cusname)
        {
            var query = BuildQuery(id, cusname);
            return query.ToList();
        }
        [HttpGet("{id}")]
        public ActionResult<Order> GetOrder(int id)
        {
            var order = orderingContext.Orders.FirstOrDefault(t => t.id == id);
            if (order == null)
                return NotFound();
            return order;
        }
        private IQueryable<Order> BuildQuery(int? id, string? cusname)
        {
            IQueryable<Order> query = orderingContext.Orders;
            if (id != null)
            {
                query = query.Where(t => t.id == id);
            }
            if (cusname != null)
            {
                query = query.Where(t => t.Customer == cusname);
            }
            return query;
        }
        [HttpPost]
        public ActionResult<Order> PostOrder(Order order)
        {
            try
            {
                orderingContext.Orders.Add(order);
                orderingContext.SaveChanges();
            }
            catch (Exception e)
            {
                String error = (e.InnerException == null) ? e.Message
                    : e.InnerException.Message;
                return BadRequest(error);
            }
            return order;
        }
        [HttpPut("{id}")]
        public ActionResult<Order> PutOrder(int id, Order order)
        {
            if (id != order.id)
                return BadRequest("Id cannot be modified!");
            try
            {
                orderingContext.Entry(order).State = EntityState.Modified;
                orderingContext.SaveChanges();
            }
            catch (Exception e)
            {
                String error = (e.InnerException == null) ? e.Message
                    : e.InnerException.Message;
                return BadRequest(error);
            }
            return NoContent();
        }
        [HttpDelete("{id}")]
        public ActionResult<Order> DeleteOrder(int id)
        {
            try
            {
                var order = orderingContext.Orders.FirstOrDefault(o => o.id == id);
                if (order != null)
                {
                    orderingContext.Remove(order);
                    orderingContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                String error = (e.InnerException == null) ? e.Message
                    : e.InnerException.Message;
                return BadRequest(error);
            }
            return NoContent();
        }
    }
}
